﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using NLog;

namespace KlavoNotifications.Storage
{
    public class NotificationStorage
    {
        private static readonly Logger _logger = LogManager.GetCurrentClassLogger();

        private const string SettingsFileName = "settings.xml";
        private const string SettingsFolderName = "KlavoNotifications";

        private NotificationStorageData _data;


        public List<string> Vocabularies
        {
            get { return Data.Vocabularies; }
        }

        public List<string> Players
        {
            get { return Data.Players; }
        }

        public bool IsCompetitions
        {
            get { return Data.IsCompetitions; }
        }

        public bool IsVocabularies
        {
            get { return Data.IsVocabularies; }
        }

        public bool IsPlayers
        {
            get { return Data.IsPlayers; }
        }

        public bool IsX2
        {
            get { return Data.IsX2; }
        }

        public bool IsX3
        {
            get { return Data.IsX3; }
        }

        public bool IsX4
        {
            get { return Data.IsX4; }
        }

        public bool IsX5
        {
            get { return Data.IsX5; }
        }

        public NotificationStorageData Data
        {
            get
            {
                if (_data == null)
                    _data = Load();
                return _data;
            }
        }

        public void Save(NotificationStorageData data)
        {
            try
            {
                string filePath = ConfigFile();

                XmlSerializer serializer = new XmlSerializer(typeof(NotificationStorageData));
                TextWriter textWriter = new StreamWriter(filePath);
                serializer.Serialize(textWriter, data);
                textWriter.Close();

                _data = null;
            }
            catch (Exception exc)
            {
                _logger.ErrorException("Save", exc);
            }
        }

        private NotificationStorageData Load()
        {
            NotificationStorageData data;
            try
            {
                string filePath = ConfigFile();

                if (String.IsNullOrEmpty(filePath) || !File.Exists(filePath))
                    return new NotificationStorageData();

                XmlSerializer deserializer = new XmlSerializer(typeof(NotificationStorageData));
                TextReader textReader = new StreamReader(filePath);
                data = (NotificationStorageData)deserializer.Deserialize(textReader);
                textReader.Close();
            }
            catch (Exception exc)
            {
                data = null;
                _logger.ErrorException("Load", exc);
            }

            return data;
        }

        private string ConfigFile()
        {
            string appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string dir = Path.Combine(appData, SettingsFolderName);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            string finalPath = Path.Combine(dir, SettingsFileName);

            return finalPath;
        }
    }
}